while .t.
       sombra(10,20,22,58,'Relatorios','Esc-Sai')
       @ 12,26 prompt ' 1 - Resumo do Faturamento '
       @ 13,26 prompt ' 2 - Retencao de INSS      '
       @ 14,26 prompt ' 3 - Relatorio Contador    '
       @ 15,26 prompt ' 4 - Retencao Federal      '
       @ 16,26 prompt ' 5 - Devedores             '
       @ 17,26 prompt ' 6 - Clientes              '
       @ 18,26 prompt ' 7 - Retencao de IRRF      '
       @ 19,26 prompt ' 8 - Declaracao de Ret-ISS '
       @ 20,26 prompt ' 9 - Declaracao de IRPJ    '
       menu to opcao
       do case
          case opcao = 1
               afatura()   
          case opcao = 2
               acompen()
          case opcao = 3
               sort on numero  to temp
               do relato3 with 'ADMINISTRACAO - '
          case opcao = 4
               sort on numero  to temp
               do relato4 with 'ADMINISTRACAO'
          case opcao = 5
               devedor()
          case opcao = 6
               acliente() 
          case opcao = 7
               sele notasa 
               sort on numero  to retirrf 
               do retirrf with 'ADMINISTRACAO - '
          case opcao = 8
               sele notasa 
               sort on numero  to retiss  
               do retiss  with 'A'
          case opcao = 9
               do retirpj with 'A'
          case lastkey() = 27
               exit
       endcase
       exit
enddo

function afatura()
   if .not. impressora()
      retu(2)
   endif
   legal(8,6,22,77,'Resumo de Faturamento')
   mmes =  '  /    '
   @ 10,8 say 'Mes/Ano   :' get mmes pict '99/9999'
   read
   if empty(ctod('01/'+mmes))
      mensa(10,'Dados invalidos',2)
      retu  
   endif
   mtitulo = .t.
   mpagina = 1
   tretencao1 := tretencao2 := tretencao4 := tiss := 0    
   tvalor := tliquido := treceita := 0
   mcancela = ''
   sele notasa    
   go top
   while .not. eof()
    if right(dtoc(emissao),7) # mmes
      skip
      loop
    endif
    if cancela = 'S'
       mcancela+=str(numero,8)
       skip
       loop
    endif
    set devi to screen
    @ 15,8 say 'Data        : ' + dtoc(emissao)
    set devi to printer
    if mtitulo
       tit_rela('RESUMO DO FATURAMENTO - ADMINISTRACAO : ' + mmes,mpagina)
       mtitulo = .f.
       @ prow(),0   say chr(15)
       @ prow()+1,0 say '  DATA DE     NOTA                       I.N.           I.N.          I.R.                                     DATA       LIQUIDO'
       @ prow()+1,0 say '  EMISSAO    FISCAL         VALOR       381/03        04/1997                      I.N.S.S.         I.S.S.    RECEBTO    RECEBIDO'
       @ prow()+1,0 say repli('=',132)
       @ prow()+1,0 say ''
    endif                               //   1234567890 1234567890
    mretencao1 = acharetencao(1)
    mretencao2 = acharetencao(2)
    mretencao4 = acharetencao(4)
    mcliente = cliente
    sele clientes
    seek mcliente  
    mpublico  = publico
    mnome     = nome
    mcgc      = cgc
    mportaria = portaria
    sele notasa
    maliquota = irrf+csll+cofins+pis        
    mtotal    = total                   
    mliquido1 = total - mretencao1
    mreceita  = federal
    if mcliente = 999
       mreceita = (total - mretencao1) * (maliquota/100)
    endif
    treceita += mreceita
    mliquido  = mtotal - (valorret1+valorret2+valorret3+federal+mreceita)
    tliquido += mliquido                 
    @ prow()+1,0  say emissao
    @ prow(),13   say numero     pict '999999'
    @ prow(),21   say mtotal     pict '99,999,999.99'
    @ prow(),35   say mretencao4 pict '999,999.99' // IN 381/03
    @ prow(),50   say mreceita   pict '999,999.99' // IN 04/1997
    @ prow(),65   say mretencao2 pict '999,999.99'
    @ prow(),80   say mretencao1 pict '999,999.99'
    @ prow(),95   say iss        pict '999,999.99'
    @ prow(),110  say recebto
    @ prow(),122  say mliquido   pict '99,999,999.99'
    @ prow()+1,0 say 'Contratante : ' + mnome + '           C.N.P.J. : ' + mcgc
    @ prow()+1,0  say ''
    tretencao1 += mretencao1
    tretencao2 += mretencao2
    tretencao4 += mretencao4
    tiss       += iss       
    tvalor     += total
    skip
    if prow() > 56
       eject
       mtitulo = .t.
       mpagina+=1
    endif
   enddo
   @ prow()+1,0  say repli('=',132)
   mlinha  = 'TOTAL' + space(12) 
   mlinha += trans(tvalor,'999,999,999.99')                
   mlinha += trans(tretencao4,'999,999,999.99') 
   mlinha += trans(treceita,'9999,999,999.99')   
   mlinha += trans(tretencao2,'9999,999,999.99') 
   mlinha += trans(tretencao1,'9999,999,999.99') 
   mlinha += trans(tiss,'999,999,999.99') + space(14)   
   mlinha += trans(tliquido,'99,999,999.99')  
   @ prow()+1,0  say mlinha
   @ prow()+1,0  say repli('=',132)
   @ prow()+1,0  say 'Notas canceladas : ' + mcancela               
   @ prow()+1,0  say repli('=',132)
   eject
   set devi to screen
retu


function acompen()
   if .not. impressora()
      retu(2)
   endif
   legal(8,6,22,77,'Retencao a compensar')
   mmes := '  /    '
   @ 11,8 say 'Competencia :' get mmes pict '99/9999'
   read
   mtotal1 := mtotal2 := mtotal3 := mtotal4 := mtotal5 := mtotal6 := mtotal7 := 0
   mtitulo = .t.
   mpagina = 1
   mtotal = 0
   sele notasa    
   go top
   while .not. eof()
    if inss # mmes
      skip
      loop
    endif
    if cancela = 'S'
*      mcancela+=str(numero,8)
       skip
       loop
    endif
    set devi to screen
    @ 15,8 say 'Data        : ' + dtoc(emissao)
    set devi to printer
    if mtitulo
       tit_rela('RETENCAO INSS SER COMPENSADA - ADMINISTRACAO : ' + mmes,mpagina)
       mtitulo = .f.
       @ prow(),0   say chr(15)
       @ prow()+1,0 say '  DATA DE     NOTA        VALOR       RETENCAO     VALOR APOS                                   RETENCAO       LIQUIDO A    GPS'
       @ prow()+1,0 say '  EMISSAO    FISCAL       BRUTO       I.N.S.S.       RETENCAO      I.R.R.F.         I.S.S.       FEDERAL         RECEBER       '
       @ prow()+1,0 say repli('=',132)
       @ prow()+1,0 say ''
    endif
    mcliente = cliente
    sele clientes
    seek mcliente  
    mpublico  = publico
    mnome     = nome
    mcgc      = cgc
    mportaria = portaria
    sele notasa
    maliquota = if(material='S',0.0585,0.0945)
    mtotal    = total
    mliquido1 = mtotal - valorret1
    mreceita  = 0
    if mpublico = 'S'
       if mportaria = 'S'
          mreceita = mliquido1 * maliquota
          mtotal6 += mreceita
       else
          mreceita = mtotal * maliquota
          mtotal6 += mreceita
       endif
    endif
    mliquido  = mtotal - (valorret1+valorret2+valorret3+mreceita)
    mtotal1+=mtotal
    mtotal2+=valorret1
    mtotal3+=mliquido1
    mtotal4+=valorret2
    mtotal5+=valorret3
    mtotal7+=mliquido 
    @ prow()+1,1  say emissao
    @ prow(),13   say numero    pict '999999'
    @ prow(),21   say mtotal    pict '9,999,999.99'
    @ prow(),36   say valorret1 pict '9,999,999.99'
    @ prow(),50   say mliquido1 pict '9,999,999.99'
    @ prow(),65   say valorret2 pict '9,999,999.99'
    @ prow(),79   say valorret3 pict '9,999,999.99'
    @ prow(),94   say mreceita  pict '9,999,999.99'
    @ prow(),110  say mliquido  pict '9,999,999.99'
    if valetransp + valealimen > 0
       @ prow()+1,0 say 'Vale-Transporte  : ' + trans(valetransp,'999,999.99')
       @ prow(),30  say 'Vale-Alimentacao : ' + trans(valealimen,'999,999.99')
    endif
    @ prow()+1,2 say 'Cliente : ' + mnome + ' C.N.P.J. : ' + mcgc                                                         
    @ prow()+1,2 say ''
    skip
    if prow() > 56
       eject
       mtitulo = .t.
       mpagina+=1
    endif
   enddo
   @ prow()+1,0  say repli('=',132)
   @ prow()+1,0  say 'TOTAl'                                       
   @ prow(),21   say mtotal1   pict '9,999,999.99'
   @ prow(),36   say mtotal2   pict '9,999,999.99'
   @ prow(),50   say mtotal3   pict '9,999,999.99'
   @ prow(),65   say mtotal4   pict '9,999,999.99'
   @ prow(),79   say mtotal5   pict '9,999,999.99'
   @ prow(),94   say mtotal6   pict '9,999,999.99'
   @ prow(),110  say mtotal7   pict '9,999,999.99'
   @ prow()+1,0  say repli('=',132)
   eject
   set devi to screen

retu



function devedor()
   if .not. impressora()
      retu(2)
   endif
   legal(8,6,22,77,'Devedores')
   mfinal = ctod('')
   @ 10,8 say 'Data Final :' get mfinal
   read
   mtitulo = .t.
   mpagina = 1
   tvalor := tliquido := treceita := 0
   mcancela = ''
   sele notasa
   seek 638
   while .not. eof()
    if .not. empty(recebto)
      skip
      loop
    endif
    if mfinal > memissao
       skip
       loop
    endif
    set devi to screen
    @ 15,8 say 'Data        : ' + dtoc(emissao)
    set devi to printer
    if mtitulo
       tit_rela('DEVEDORES - ADMINISTRACAO',mpagina)
       mtitulo = .f.
       @ prow(),0   say chr(15)
       @ prow()+1,0 say '   DATA DE     NOTA '
       @ prow()+1,0 say '   EMISSAO    FISCAL           VALOR       CLIENTE                                                       CONTATO      TELEFONE'
       @ prow()+1,0 say repli('-',132)
    endif                               //   1234567890 1234567890
    mcliente = cliente
    sele clientes
    seek mcliente  
    mnome     = nome
    mcontato  = left(contato,14)
    mfone     = fone1 
    sele notasa
    mlinha  = dtoc(emissao) + ' | ' + str(numero,8) + ' | ' + trans(total,'999999,999.99')
    mlinha += ' | ' + mnome + ' | ' + mcontato + ' |  ' + mfone + '  | '
    @ prow()+1,0  say mlinha
    tvalor     += total
    skip
    if prow() > 56
       eject
       mtitulo = .t.
       mpagina+=1
    endif
   enddo
   @ prow()+1,0  say repli('-',132)
   @ prow()+1,0  say 'TOTAl'                                       
   @ prow(),21   say tvalor     pict '99999,999,999.99'
   @ prow()+1,0  say repli('=',132)
   eject
   set devi to screen
retu


function acliente()
   if .not. impressora()
      retu(2)
   endif
   legal(8,6,22,77,'Relatorio por Cliente')
   minicio := mfinal := ctod('')
   @ 10,8 say 'Inicio    :' get minicio
   @ 11,8 say 'Final     :' get mfinal 
   @ 12,8 say 'Cliente   :' get mcliente pict '999999'
   read
   if empty(minicio)
      retu  
   endif
   sele clientes
   seek mcliente  
   mnome     = nome
   macofins := .03
   mapis    := .0065
   mair     := .012
   macs     := .01
   @ 13,8 say 'Cofins    :' get macofins pict '99.9999'
   @ 14,8 say 'PIS       :' get mapis    pict '99.9999'
   @ 15,8 say 'I.R.      :' get mair     pict '99.9999'
   @ 16,8 say 'C.S.      :' get macs     pict '99.9999'
   read
   mtitulo = .t.
   mpagina = 1
   tcofins := tpis := tpis := tir := tcs := timpostos := ttotal := 0
   gcofins := gpis := gpis := gir := gcs := gimpostos := gtotal := 0
   sele notasa    
   go top
   while .not. eof()
    if mcliente # cliente
      skip
      loop
    endif
    if cancela = 'S'
       skip
       loop
    endif
    if emissao < minicio .or. emissao > mfinal
       skip
       loop
    endif
    set devi to screen
    @ 15,8 say 'Data        : ' + dtoc(emissao)
    set devi to printer
    if mtitulo
       tit_rela('RELATORIO POR CLIENTE - ADMINISTRACAO',mpagina)
       mtitulo = .f.
       @ prow(),0   say chr(15)
       @ prow()+1,0 say '   DATA DE     NOTA       DATA    VALOR   *                      VALOR RETIDO                           *                '
       @ prow()+1,0 say '   EMISSAO   FISCAL   RETENCAO      N.F.  *     COFINS         PIS        I.R.        C.S.        TOTAL *  ORGAO RETENTOR'
       @ prow()+1,0 say repli('=',132)
       @ prow()+1,0 say ''
    endif
    sele notasa
    mcofins   = total * macofins
    mpis      = total * mapis  
    mir       = total * mair 
    mcs       = total * macs
    mliquido  = mcofins+mpis+mir+mcs
    ttotal   += total
    tcofins  += mcofins
    tpis     += mpis
    tir      += mir
    tcs      += mcs
    timpostos+= mliquido
    @ prow()+1,0  say dtoc(emissao) + str(numero,8) + '  ' + dtoc(recebto) + trans(total,'999999,999.99')
    @ prow(),44   say mcofins   pict '999,999.99'
    @ prow(),56   say mpis      pict '999,999.99'
    @ prow(),68   say mir       pict '999,999.99'
    @ prow(),80   say mcs       pict '999,999.99'
    @ prow(),93   say mliquido  pict '999,999.99'
    @ prow(),104  say trans(mcliente,'999999') + ' ' + left(mnome,20)
    skip
    if prow() > 56
       eject
       mtitulo = .t.
       mpagina+=1
    endif
   enddo
   @ prow()+1,0  say repli('=',132)
   @ prow()+1,0  say 'Total'                                           
   @ prow(),32   say ttotal    pict '9999,999.99'
   @ prow(),44   say tcofins   pict '999,999.99'
   @ prow(),56   say tpis      pict '999,999.99'
   @ prow(),68   say tir       pict '999,999.99'
   @ prow(),80   say tcs       pict '999,999.99'
   @ prow(),93   say timpostos pict '999,999.99'
   @ prow()+1,0  say repli('=',132)
   set devi to screen
retu


